package cn.yi.medicalsys.service.impl;

import java.util.List;

import cn.yi.medicalsys.dao.DoctorDao;
import cn.yi.medicalsys.dao.impl.DoctorDaoImpl;
import cn.yi.medicalsys.pojo.Doctor;
import cn.yi.medicalsys.pojo.PageBean;
import cn.yi.medicalsys.service.DoctorService;

public class DoctorServiceImpl implements DoctorService {
	DoctorDao doctor = new DoctorDaoImpl();

	@Override
	public int findTotalRecord(String doctorName, String office) {
		int totalCount = -1;
		try {
			totalCount = doctor.findTotalCount(doctorName, office);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return totalCount;
	}

	@Override
	public PageBean<Doctor> findDoctorByPage(int pageNo, int pageSize,
			String doctorName, String office) {
		PageBean<Doctor> pageBean = new PageBean<Doctor>();
		// 设置每页的记录数 先
		pageBean.setPageSize(pageSize);
		int totalCount = this.findTotalRecord(doctorName, office);
		// 设置总页码 总页数在pageBean 以计算
		pageBean.setTotalCount(totalCount);
		// 设置当前页码数
		pageBean.setCurrPageNo(pageNo);
		// 获取偏移量
		int offSet = (pageNo - 1) * pageSize;
		List<Doctor> list = null;
		try {
			list = doctor.findDoctorByOffset(offSet, pageSize, doctorName,
					office);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 设置集合
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public int findTotalPage(int pageSize, String doctorName, String office) {
		int totalCount = this.findTotalRecord(doctorName, office);
		int totalPageCount = 0;
		if (totalCount >= 0) {
			int temp = totalCount / pageSize;
			totalPageCount = totalCount % pageSize == 0 ? temp : temp + 1;
		}
		return totalPageCount;

	}

	@Override
	public boolean updateDoctor(Integer doctAge, String appellation,
			String doctPhone, String doctEmail, Integer did,Integer officeid) {
		int row=-1;
		try {
			row = doctor.updateDoctor(doctAge, appellation, doctPhone,
					doctEmail, did,officeid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (row == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addDoctor(Doctor docto) {
		int row=-1;
		try {
			row=doctor.addDoctor(docto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(row==1){
			return true;
		}
		return false;
	}
	
   public static void main(String[] args) {
		DoctorServiceImpl sh = new DoctorServiceImpl();
		System.out.println("111");
		System.out.println(sh.findTotalPage(1, null, "内科"));
		System.out.println(sh.findDoctorByPage(1, 5, null, "内科")
				.getTotalPageCount());
		Doctor docto=new Doctor(Integer.valueOf(1),Integer.valueOf(1),"占领","女",Integer.valueOf(19),"18339689663","教授","105131@qq.com","null","jpg");
		System.out.println(sh.addDoctor(docto));
		System.out.println(sh.findDoctorByNo(1).getDoctName());
		System.out.println(sh.deleteDoctor(13));
	}

	@Override
	public Doctor findDoctorByNo(Integer did) {
		Doctor docto =null;
		try {
			docto=doctor.findOfficeByNo(did);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return docto;
	}

	@Override
	public boolean deleteDoctor(Integer did) {
		int row=-1;
		try {
			row=doctor.deleteDoctor(did);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(row==1){
			return true;
		}
		return false;
	}
}
